package com.jcfk.eam.controller.bd;

import cn.hutool.core.util.StrUtil;
import com.jcfk.common.business.enums.AvailableStatus;
import com.jcfk.common.business.enums.DeleteFlag;
import com.jcfk.common.business.log.annotation.RequestLogHeader;
import com.jcfk.common.business.utils.DataBindHelper;
import com.jcfk.common.security.service.TokenService;
import com.jcfk.common.web.domain.*;

import com.jcfk.eam.domain.dto.base.EamTeamDTO;
import com.jcfk.eam.domain.dto.base.EamTeamMemberDTO;
import com.jcfk.eam.domain.query.base.EamTeamMemberQuery;
import com.jcfk.eam.domain.query.base.EamTeamQuery;
import com.jcfk.eam.service.base.EamTeamMemberService;
import com.jcfk.eam.service.base.EamTeamService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

import com.jcfk.common.core.utils.ExcelUtils;

import java.util.List;

/**
 * 运维班组
 *
 * @author libinghai
 * @email 35738025@gqq.com
 * @date 2024-08-14
 */
@Api(tags = {"运维班组管理"})
@RestController
@RequestMapping("eamTeam")
public class EamTeamController {
    @Autowired
    private EamTeamService eamTeamService;

    @Autowired
    private EamTeamMemberService eamTeamMemberService;

    @Autowired
    private TokenService tokenService;

    /**
     * 分页列表
     */
    @ApiOperation("分页查询运维班组")
    @PostMapping("/queryByPage")
    public RetPage<EamTeamDTO> queryByPage(@RequestBody ReqPageParam<EamTeamQuery> requestParam) {
        DataPage<EamTeamDTO> page = new DataPage<EamTeamDTO>((long) requestParam.getCurrent(),
                (long) requestParam.getSize(),
                requestParam.getSort().getOrderItem(),
                EamTeamDTO.class);
        page = eamTeamService.queryByPage(page, requestParam.getData());

        return new RetPage<>(page);
    }


    /**
     * 运维班组
     * 根据id 查询详情
     */
    @ApiOperation("查询运维班组")
    @GetMapping("/get/{id}")
    public RetResult<EamTeamDTO> get(@PathVariable("id") String id) {
        EamTeamDTO item = eamTeamService.get(id);
        return RetResult.ok(item);
    }

    /**
     * 添加/更新管理运维班组
     */
    @ApiOperation("保存运维班组")
    @PostMapping("/save")
    @RequestLogHeader(modular = "运维班组管理", businessType = "保存")
    public RetResult<?> save(@RequestBody @Valid EamTeamDTO eamTeam) {
        // 数据校验
        RetResult ret = eamTeamService.isValid(eamTeam);
        if (!ret.getCode().equals(RetResult.SUCCESS)) {
            return ret;
        }

        if (StrUtil.isEmpty(eamTeam.getId())) {
            // 新增
            eamTeamService.insert(eamTeam);
        } else {
            // 更新
            eamTeamService.update(eamTeam);
        }
        return RetResult.ok();

    }

    /**
     * 批量删除运维班组
     */
    @ApiOperation("批量删除运维班组")
    @PostMapping("/delete")
    @RequestLogHeader(modular = "运维班组管理", businessType = "批量删除")
    public RetResult<?> delete(@RequestBody EamTeamDTO eamTeam) {
        return RetResult.ok(eamTeamService.logicDeleteList(eamTeam));
    }

    /**
     * 删除运维班组
     */
    @ApiOperation("删除运维班组")
    @PostMapping("/deleteItem/{id}")
    @RequestLogHeader(modular = "运维班组管理", businessType = "删除")
    public RetResult<?> deleteItem(@PathVariable("id") String id) {
        eamTeamService.deleteitem(id);
        return RetResult.ok();
    }

    /**
     * 获取班组列表
     */
    @ApiOperation("获取班组列表")
    @PostMapping("/getAvailableList")
    public RetResult<?> getAvailableList(@RequestBody EamTeamQuery query) {
        query.setEnabled(1);
        query.setDeleted(0);
        List<EamTeamDTO> list = eamTeamService.getList(query);

        return RetResult.ok(list);
    }

    /**
     * 获取班组成员列表
     *
     * @param dto
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "MesProducePlanQuery", name = "query", value = "", required = true)
    })
    @ApiOperation(value = "获取班组成员列表", notes = "获取班组成员列表", httpMethod = "POST")
    @PostMapping("getDetails")
    public RetResult<List<EamTeamMemberDTO>> getDetails(@RequestBody EamTeamDTO dto) {

        List<EamTeamMemberDTO> list = eamTeamMemberService.getDetails(dto);

        return RetResult.ok(list);
    }

    /**
     * 查询运维班组
     */
    @ApiOperation("查询运维班组")
    @PostMapping("/getList")
    public RetResult<?> getList(@RequestBody EamTeamQuery requestParam) {
        if (requestParam.getEnabled() == null) {
            requestParam.setEnabled(AvailableStatus.ENABLE.getValue());
        }
        if (requestParam.getDeleted() == null) {
            requestParam.setDeleted(DeleteFlag.NOTDELETED.getValue());
        }
        return RetResult.ok(eamTeamService.getList(requestParam));
    }

    /**
     * 根据当前用户获取班组
     * @return
     */
    @PostMapping("/getListByLogin")
    public RetResult<?> getListByLogin(){
        LoginInfo loginInfo=tokenService.getLoginInfo();
        String empNo=loginInfo.getParameters().get("empNo");
        if(StrUtil.isEmpty(empNo)){
            return this.getAvailableList(new EamTeamQuery());
        }
        List<EamTeamDTO> list=eamTeamService.getListByEmpNo(empNo);

        return RetResult.ok(list);
    }
}
